package com.itheima.web.controller;


import com.itheima.common.utils.UtilFuns;
import com.itheima.domain.system.Module;
import com.itheima.domain.system.User;
import com.itheima.service.system.IModuleService;
import com.itheima.service.system.IUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class LoginController extends BaseController{

    @Autowired
    private IUserService userService;

    @Autowired
    private IModuleService moduleService;


    /**
     * 使用apache shiro的登录方式登录
     * @param email
     * @param password
     * @return
     */
    @RequestMapping("/login")
    public String login(String email,String password) {

        //判断当前用户是否已经登录
        User user =(User) session.getAttribute("user");

        if(user!=null){
            return "home/main";

        }        //判断用户是否为空
        if(UtilFuns.isEmpty(email)) {
            return "redirect:/login.jsp";
        }

        try {
            //获取到Subject
            Subject subject = SecurityUtils.getSubject();

            //创建带有用户名和密码的通行证
            UsernamePasswordToken token = new UsernamePasswordToken(email, password);

            //使用Subject的login方法实现登录
            subject.login(token);//-----此处进行密码和用户名和数据库的匹配

            //获取当前登录用户的信息
            user = (User) subject.getPrincipal();
            //登录成功，存入session域中
            session.setAttribute("user",user);

            //获取登录用户对应的菜单
            List<Module> moduleList = moduleService.findModuleByUser(user);
            session.setAttribute("modules",moduleList);

            return "home/main";
        } catch (Exception e) {
            request.setAttribute("error","用户名或密码错误");
            return "forward:/login.jsp";
        }
    }

    //退出
    @RequestMapping(value = "/logout",name="用户退出")
    public String logout(){
        //session.removeAttribute("user");
        SecurityUtils.getSubject().logout();
        return "redirect:login.jsp";
    }

    @RequestMapping("/home")
    public String home(){
        return "home/home";
    }



    /**
     * 传统处理登录功能
     * @param
     * @param
     * @return
     */
	/*@RequestMapping("/login")
	public String login(String email,String password) {

        //判断用户是否为空
        if(UtilFuns.isEmpty(email)) {
            return "redirect:/login.jsp";
        }

        //根据邮箱查询用户
        User user = userService.findByEmail(email);

        //判断用户是否存在
        if(user == null){
            request.setAttribute("error","登录邮箱或密码不正确！");
            return "forward:/login.jsp";
        }
        //判断密码是否匹配
        if(!user.getPassword().equals(password)){
            request.setAttribute("error","登录邮箱或密码不正确！");
            return "forward:/login.jsp";
        }
        session.setAttribute("user",user);

        //获取登录用户对应的菜单
        List<Module> moduleList = moduleService.findModuleByUser(user);
        session.setAttribute("modules",moduleList);

        return "home/main";
	}*/




}
